﻿namespace WLFramework.Data
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    
    /// <summary>
    /// IDbCommand扩展方法
    /// </summary>
    public static class DbCommandExtensions
    {
        /// <summary>
        /// 执行查询命令，获取DataTable结果
        /// </summary>
        /// <param name="cmd">IDbCommand对象</param>
        /// <returns>查询结果</returns>
        public static DataTable ToDataTable(this IDbCommand cmd)
        {
            Shield.AssertNotNull(ref cmd, "cmd");

            using (cmd)
            using(var reader = cmd.ExecuteReader())
            {
                return reader.ToDataTable();
            }
        }

        /// <summary>
        /// 执行查询命令，获取实体类集合
        /// </summary>
        /// <typeparam name="TEntity">实体类类型</typeparam>
        /// <param name="cmd">IDbCommand对象</param>
        /// <returns>查询结果</returns>
        public static IList<TEntity> ToList<TEntity>(this IDbCommand cmd)
            where TEntity : new()
        {
            Shield.AssertNotNull(ref cmd, "cmd");

            using (var reader = cmd.ExecuteReader())
            {
                return reader.ToList<TEntity>();
            }
        }

        /// <summary>
        /// 执行查询命令， 获取第一列的所有值
        /// </summary>
        /// <typeparam name="TValue">列的类型</typeparam>
        /// <param name="cmd">IDbCommand对象</param>
        /// <returns>查询结果</returns>
        public static IList<TValue> ToFirstColumnList<TValue>(this IDbCommand cmd)
        {
            Shield.AssertNotNull(ref cmd, "cmd");

            using (var reader = cmd.ExecuteReader())
            {
                return reader.ToFirstColumnList<TValue>();
            }
        }

        /// <summary>
        /// 执行查询命令， 根据指定列的序号获取该列的所有值
        /// </summary>
        /// <typeparam name="TValue">列的类型</typeparam>
        /// <param name="cmd">IDbCommand对象</param>
        /// <param name="index">列序号</param>
        /// <returns>查询结果</returns>
        public static IList<TValue> ToListByIndex<TValue>(this IDbCommand cmd, int index)
        {
            Shield.AssertNotNull(ref cmd, "cmd");

            using (var reader = cmd.ExecuteReader())
            {
                return reader.ToListByIndex<TValue>(index);
            }
        }

        /// <summary>
        /// 执行查询命令， 根据指定列的名称获取该列的所有值
        /// </summary>
        /// <typeparam name="TValue">列的类型</typeparam>
        /// <param name="cmd">IDbCommand对象</param>
        /// <param name="name">列名称</param>
        /// <returns>查询结果</returns>
        public static IList<TValue> ToListByColumnName<TValue>(this IDbCommand cmd, string name)
        {
            Shield.AssertNotNull(ref cmd, "cmd");
            Shield.AssertNotNullOrEmpty(ref name, "name");

            using (var reader = cmd.ExecuteReader())
            {
                return reader.ToListByColumnName<TValue>(name);
            }
        }

        /// <summary>
        /// 执行查询命令， 获取第一行第一列的结果
        /// </summary>
        /// <typeparam name="TValue">结果的类型</typeparam>
        /// <param name="cmd">IDbCommand对象</param>
        /// <returns>查询结果</returns>
        public static TValue ExecuteScalar<TValue>(this IDbCommand cmd)
        {
            Shield.AssertNotNull(ref cmd, "cmd");
            return (TValue)cmd.ExecuteScalar();
        }

        /// <summary>
        /// 像该IDbCommand对象添加参数
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public static void AddParameter(this IDbCommand cmd, string name, object value)
        {
            Shield.AssertNotNull(ref cmd, "cmd");

            var parameter = cmd.CreateParameter();
            parameter.ParameterName = name;
            parameter.Value = value;
            cmd.Parameters.Add(parameter);
        }
    }
}
